package Vista;

import org.apache.struts2.ServletActionContext;

import Dominio.Dom_Fachada;
import Dominio.DominioException;
import Dominio.PerfilData;
import Dominio.Rol;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {
	private static final long serialVersionUID = 1L;

	// User data
	private String nick;
	private String pass;

	// User
	private PerfilData _user;

	public String execute() {
		try {
			_user = new Dom_Fachada().loginUser(nick, pass);
			Rol rol = _user.get_usuario().get_rol();
			ServletActionContext.getRequest().getSession()
					.setAttribute("loggedUser", _user);
			if (rol == Rol.CLIENTE)
				return "success_c";
			if (rol == Rol.ADMINISTRADOR)
				return "success_a";
			if (rol == Rol.TRABAJADOR)
				return "success_t";
		} catch (DominioException d) {
			addFieldError("nif", "Datos existentes.");
			return INPUT;
		} catch (Exception e) {
			addFieldError("nick", "El usuario no existe.");
			return INPUT;
		}
		return ERROR;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

}
